package com.qqtech.qquan.push.service;

import java.util.Set;

import com.qqtech.core.frame.model.Result;
import com.qqtech.qquan.push.enums.PushTypeEnum;
import com.qqtech.qquan.push.helper.JgPushSettings;
import com.qqtech.qquan.push.helper.PushCfg;
import com.qqtech.qquan.push.helper.PushPayloadHelper;
import com.qqtech.qquan.push.model.PushLogVo;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.PushPayload;

public class PushJgServiceImpl implements PushJgService {

	private JPushClient jPushClient;

	public PushJgServiceImpl(JgPushSettings jgPushSettings) {
		this.jPushClient = new JPushClient(jgPushSettings.getMasterSecret(), jgPushSettings.getAppKey());
	}

	@Override
	public Result sendPush(PushLogVo pushLogVo) {
		Result result = new Result();
		try {
			PushPayload pushPayload = null;
			if (PushTypeEnum.getInstance(pushLogVo.getType()) == null) {
				pushPayload = PushPayloadHelper.buildAlertNMessage(pushLogVo);
			} else {
				if (PushTypeEnum.ALERT.getCode() == pushLogVo.getType().intValue()) {
					pushPayload = PushPayloadHelper.buildAlert(pushLogVo);
				} else if (PushTypeEnum.MESSAGE.getCode() == pushLogVo.getType().intValue()) {
					pushPayload = PushPayloadHelper.buildMessage(pushLogVo);
				} else if (PushTypeEnum.ALERTNMESSAGE.getCode() == pushLogVo.getType().intValue()) {
					pushPayload = PushPayloadHelper.buildAlertNMessage(pushLogVo);
				}
			}
			if (pushPayload != null) {
				if (jPushClient == null) {
					PushCfg instance = PushCfg.getInstance();
					jPushClient = new JPushClient(instance.getValue("masterSecret"), instance.getValue("appKey"));
				}
				PushResult pushResult = jPushClient.sendPush(pushPayload);
				if (pushResult != null) {
					if (pushResult.msg_id != 0) {
						result.put("msgId", "" + pushResult.msg_id);
					}
					result.put("pushResult", pushResult);
				}
			} else {
				result.addErr("推送对象为空");
			}
			return result;
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
			result.addErr("推送发生异常");
			return result;
		}
	}

	@Override
	public void clearDeviceTag(String registrationId) {
		try {
			jPushClient.updateDeviceTagAlias(registrationId, false, true);
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void clearDeviceAlias(String registrationId) {
		try {
			jPushClient.updateDeviceTagAlias(registrationId, true, false);
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void updateDeviceTagAlias(String registrationId, String alias, Set<String> tagsToAdd,
			Set<String> tagsToRemove) {
		try {
			jPushClient.updateDeviceTagAlias(registrationId, alias, tagsToAdd, tagsToRemove);
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
		}
	}
}
